package com.zxbit.flash.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zxbit.flash.R;
import com.zxbit.flash.base.BaseRecyclerAdapter;
import com.zxbit.flash.common.Constant;
import com.zxbit.flash.bean.OrderItemData;
import com.zxbit.flash.utils.DateUtil;
import com.zxbit.flash.utils.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 订单的适配器
 */
public class BillAdapter extends BaseRecyclerAdapter<OrderItemData, BillAdapter.BillViewHolder> {

    private Context context;
    private OnRepaymentClickListener repaymentClickListener;

    public BillAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @NonNull
    @Override
    public BillViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new BillViewHolder(inflater.inflate(R.layout.item_of_bill_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull BillViewHolder holder, int position) {
        final OrderItemData orderItem = mDatas.get(position);
        holder.tvOrderId.setText(orderItem.getOrderId());
        // 申请金额 产品周期
        holder.tvOrderCashNum.setText(StringUtils.getDotMoneyStr(String.valueOf(orderItem.getApprovalMoney())));
        holder.tvDeadline.setText(orderItem.getProductTerm());

        setBillType(holder, orderItem);


        // 点击还款
        holder.tvRepayment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (repaymentClickListener != null) {
                    repaymentClickListener.onRepaymentClick(view, orderItem.getOrderId(), String.valueOf(orderItem.getRepayMoney()));
                }
            }
        });

    }

    /**
     * 审批状态:审核中1 已通过,待放款2 已放款,待还款 3 已拒绝 4 已结清5 已逾期6 已展期7
     */
    private void setBillType(BillViewHolder holder, OrderItemData orderItem) {
        String typeText = "";
        switch (orderItem.getOrderStatus()) {
            case Constant.ORDER_STATUS_AUDITING:
                //1 审核中
                typeText = getStringResource(R.string.bill_type_auditing);
                holder.tvBillType.setTextColor(context.getResources().getColor(R.color.type_auditing));
                holder.tvOrderCashNum.setText(StringUtils.getDotMoneyStr(String.valueOf(orderItem.getApplyMoney())));

                holder.relBorrowTime.setVisibility(View.GONE);
                holder.relRepaymentTime.setVisibility(View.GONE);
                holder.relOverdueTime.setVisibility(View.GONE);
                holder.relOverdueMoney.setVisibility(View.GONE);
                holder.tvRepayment.setVisibility(View.GONE);
                holder.tvHint.setVisibility(View.INVISIBLE);
                break;
            case Constant.ORDER_STATUS_WAITLEND:
                //2 已通过,待放款
                typeText = getStringResource(R.string.bill_type_wait_lend);
                holder.tvBillType.setTextColor(context.getResources().getColor(R.color.type_auditing));
                holder.tvOrderCashNum.setText(StringUtils.getDotMoneyStr(String.valueOf(orderItem.getApplyMoney())));

                holder.relBorrowTime.setVisibility(View.GONE);
                holder.relRepaymentTime.setVisibility(View.GONE);
                holder.relOverdueTime.setVisibility(View.GONE);
                holder.relOverdueMoney.setVisibility(View.GONE);
                holder.tvRepayment.setVisibility(View.GONE);
                holder.tvHint.setVisibility(View.INVISIBLE);
                break;
            case Constant.ORDER_STATUS_LENDING:
                // 待还款
                typeText = getStringResource(R.string.bill_type_lending);
                holder.tvBillType.setTextColor(context.getResources().getColor(R.color.type_lending));

                holder.relBorrowTime.setVisibility(View.VISIBLE);
                holder.relRepaymentTime.setVisibility(View.VISIBLE);
                holder.relOverdueTime.setVisibility(View.GONE);
                holder.relOverdueMoney.setVisibility(View.GONE);
                holder.tvRepayment.setVisibility(View.VISIBLE);
                holder.tvHint.setVisibility(View.VISIBLE);

                holder.tvHint.setText(R.string.repayment_hint1);
                holder.tvBorrowTime.setText(DateUtil.getDateTime(Long.parseLong(orderItem.getCrtTime())));
                holder.tvRepaymentTime.setText(orderItem.getLastPayDate());
                break;
            case Constant.ORDER_STATUS_REJECT:
                // 已拒绝
                typeText = getStringResource(R.string.bill_type_reject);
                holder.tvBillType.setTextColor(context.getResources().getColor(R.color.type_reject));
                holder.tvOrderCashNum.setText(StringUtils.getDotMoneyStr(String.valueOf(orderItem.getApplyMoney())));

                holder.relBorrowTime.setVisibility(View.GONE);
                holder.relRepaymentTime.setVisibility(View.GONE);
                holder.relOverdueTime.setVisibility(View.GONE);
                holder.relOverdueMoney.setVisibility(View.GONE);
                holder.tvRepayment.setVisibility(View.GONE);
                holder.tvHint.setVisibility(View.INVISIBLE);
                break;
            case Constant.ORDER_STATUS_CLOSEOUT:
                // 已结清
                typeText = getStringResource(R.string.bill_type_closeout);
                holder.tvBillType.setTextColor(context.getResources().getColor(R.color.type_closeout));

                holder.relBorrowTime.setVisibility(View.GONE);
                holder.relRepaymentTime.setVisibility(View.GONE);
                holder.relOverdueTime.setVisibility(View.GONE);
                holder.relOverdueMoney.setVisibility(View.GONE);
                holder.tvRepayment.setVisibility(View.GONE);
                holder.tvHint.setVisibility(View.INVISIBLE);
                break;
            case Constant.ORDER_STATUS_OVERDUE:
                // 已逾期
                typeText = getStringResource(R.string.bill_type_overdue);
                holder.tvBillType.setTextColor(context.getResources().getColor(R.color.type_overdue));

                holder.relBorrowTime.setVisibility(View.VISIBLE);
                holder.relRepaymentTime.setVisibility(View.VISIBLE);
                holder.relOverdueTime.setVisibility(View.VISIBLE);
                holder.relOverdueMoney.setVisibility(View.VISIBLE);
                holder.tvRepayment.setVisibility(View.VISIBLE);
                holder.tvHint.setVisibility(View.VISIBLE);

                holder.tvHint.setText(R.string.repayment_hint2);
                holder.tvBorrowTime.setText(DateUtil.getDateTime(Long.parseLong(orderItem.getCrtTime())));
                holder.tvRepaymentTime.setText(orderItem.getLastPayDate());
                holder.tvOverdueTime.setText(orderItem.getOverdueDay() + getStringResource(R
                        .string.bill_days_unit));
                holder.tvOverdueMoney.setText(StringUtils.getDotMoneyStr(orderItem.getOverdueAmt()) + getStringResource(R
                        .string.money_unit_hint));
                break;
        }
        holder.tvBillType.setText(typeText);
    }


    private String getStringResource(int strResourceId) {
        return context.getResources().getString(strResourceId);
    }

    static class BillViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_order_id)
        TextView tvOrderId;
        @BindView(R.id.tv_bill_type)
        TextView tvBillType;
        @BindView(R.id.tv_order_cash_num)
        TextView tvOrderCashNum;
        @BindView(R.id.tv_deadline)
        TextView tvDeadline;
        @BindView(R.id.relBorrowTime)
        RelativeLayout relBorrowTime;
        @BindView(R.id.relRepaymentTime)
        RelativeLayout relRepaymentTime;
        @BindView(R.id.relOverdueTime)
        RelativeLayout relOverdueTime;
        @BindView(R.id.relOverdueMoney)
        RelativeLayout relOverdueMoney;
        @BindView(R.id.tvRepayment)
        TextView tvRepayment;
        @BindView(R.id.tvHint)
        TextView tvHint;
        @BindView(R.id.tvBorrowTime)
        TextView tvBorrowTime;
        @BindView(R.id.tvRepaymentTime)
        TextView tvRepaymentTime;
        @BindView(R.id.tvOverdueTime)
        TextView tvOverdueTime;
        @BindView(R.id.tvOverdueMoney)
        TextView tvOverdueMoney;
        @BindView(R.id.root_container_view)
        LinearLayout rootContainerView;

        BillViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public void setClickListener(OnRepaymentClickListener listener) {
        this.repaymentClickListener = listener;
    }

    public interface OnRepaymentClickListener {
        void onRepaymentClick(View view, String orderId, String repayMoney);
    }
}
